package com.eco.netty;

import com.eco.controller.LightController;
import com.eco.utils.ApplicationContextProvider;
import com.eco.utils.NettyCtxMap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/* loaded from: newfishway-1.0.0-SNAPSHOT.jar:BOOT-INF/classes/com/eco/netty/KndHexHandlerJK.class */
public class KndHexHandlerJK extends ChannelInboundHandlerAdapter {
    private static final Logger log = LoggerFactory.getLogger((Class<?>) KndHexHandlerJK.class);
    private NettyClient nettyClient;
    private LightController lightController;
    private static final String START_CHAR = "00 01 00 00 00 06 FF 05 00";
    private static final String END_CHAR = "00";

    public KndHexHandlerJK() {
        new ApplicationContextProvider();
        this.nettyClient = (NettyClient) ApplicationContextProvider.getBean(NettyClient.class);
        new ApplicationContextProvider();
        this.lightController = (LightController) ApplicationContextProvider.getBean(LightController.class);
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleState state = ((IdleStateEvent) evt).state();
            if (state == IdleState.READER_IDLE) {
                if (this.nettyClient.getSensor4EventLoopGroup() != null) {
                    this.nettyClient.shutdownKndJK();
                } else {
                    ctx.channel().close();
                }
                NettyCtxMap.delByKey("in");
                return;
            }
            return;
        }
        super.userEventTriggered(ctx, evt);
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("knd-jk有新连接");
        NettyCtxMap.add("in", ctx);
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        String host = this.nettyClient.getJkKndHost();
        int port = this.nettyClient.getJkKndPort();
        log.error("与knd-jk服务" + host + ":" + port + "连接断开!");
        ctx.close();
        this.nettyClient.shutdownKndJK();
        NettyCtxMap.delByKey("in");
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelInboundHandler
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg != null) {
            try {
                String jkKndMsg = msg.toString().trim();
                log.info("knd-jk服务端返回的消息：{}", jkKndMsg);
                if (StringUtils.startsWith(jkKndMsg, START_CHAR) && StringUtils.endsWith(jkKndMsg, END_CHAR)) {
                    log.info("向knd-jk发送指令成功返回");
                } else {
                    log.error("向knd-jk发送指令失败");
                }
            } catch (Exception e) {
                log.error(e.toString());
            }
        }
    }

    @Override // io.netty.channel.ChannelInboundHandlerAdapter, io.netty.channel.ChannelHandlerAdapter, io.netty.channel.ChannelHandler, io.netty.channel.ChannelInboundHandler
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error("knd-jk连接异常");
        ctx.close();
        NettyCtxMap.delByKey("in");
    }
}